/*
 * MIDPCanvas.java
 *
 * Created on den 18 november 2008, 09:44
 */

package hello;

import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.midlet.*;

/**
 *
 * @author  Student
 * @version
 */
class GameActionCanvas extends Canvas implements CommandListener
{
  private Command cmExit;          // Exit midlet
  private String keyText = null;    // Key code text
  private Midlet midlet;
  private Image image;
  private int pageNrPhone = 1;
  private int pageNrScreen = 1;
  // Numbers of slides of the presentation
  private int maxPages = 5;

  /*--------------------------------------------------
  * Constructor
  *-------------------------------------------------*/
  public GameActionCanvas(Midlet midlet)
  {
    this.midlet = midlet;
    
    // Create exit command & listen for events
    cmExit = new Command("Exit", Command.EXIT, 1);
    addCommand(cmExit);
    setCommandListener(this);
    // load image
    try {
      image = Image.createImage ("/bilder/1.PNG");
    }
    catch (IOException e) {
      throw new RuntimeException ("Unable to load Image: "+e);
    }
  } 

  /*--------------------------------------------------
  * Paint the text representing the key code 
  *-------------------------------------------------*/
  protected void paint(Graphics g)
  {
    // Clear the background (to white)
    g.setColor(255, 255, 255);
    g.fillRect(0, 0, getWidth(), getHeight());
    
    // Set color and draw text
    if (keyText != null)
    {
      // Draw with black pen
      g.setColor(0, 0, 0);
      // Center the text
      
      
      g.drawString(keyText, getWidth()/2, getHeight()/2, Graphics.TOP | Graphics.HCENTER);
    }
     try {
      image = Image.createImage ("/bilder/" + pageNrPhone + ".PNG");
    }
    catch (IOException e) {
      throw new RuntimeException ("Unable to load Image: "+e);
    }
    //draw image
    g.drawImage (image, getWidth () / 2, getHeight () / 2, 
           Graphics.HCENTER | Graphics.VCENTER);
  }

  /*--------------------------------------------------
  * Command event handling
  *-------------------------------------------------*/  
  public void commandAction(Command c, Displayable d)
  {
    if (c == cmExit)
      midlet.exitMIDlet();
  }

  /*--------------------------------------------------
  * Game action event handling
  * A game action will be converted into a key code 
  * and handed off to this method
  *-------------------------------------------------*/  
  protected void keyPressed(int keyCode)
  {
      switch (getGameAction(keyCode))
    {
      // Place logic of each action inside the case
        case FIRE: {
            keyText = "5";
        } break;
        case UP: {
            keyText = "2";
        } break;
        case DOWN: {
            keyText = "8";
        } break;
        case LEFT: {
            keyText = "4";
            if (pageNrScreen > 1) {
                pageNrScreen--;
                pageNrPhone = pageNrScreen;
            }
        } break;
        case RIGHT:{
            keyText = "6";
            if (pageNrScreen < maxPages) {
                pageNrScreen++;
                pageNrPhone = pageNrScreen;
            }
        } break;
        case GAME_A: {
            keyText = "7";
            if (pageNrPhone > 1) {
                pageNrPhone--;
            }
        } break;
        case GAME_B: {
            keyText = "9";
            if (pageNrPhone < maxPages) {
                pageNrPhone++;
            }
        } break;
        case GAME_C: {
            keyText = "star";
            pageNrPhone = 5;
            midlet.setDisplayList1();
        } break;
        case GAME_D: {
            keyText = "fyrkant";
            pageNrScreen = pageNrPhone;
        } break;
      default:
        // Print the text of the game action
          System.out.println("keyCode = " + keyCode);
          System.out.println("KEY_NUM2 = " + KEY_NUM2);
        keyText = getKeyName(keyCode);
    }        
    repaint();
  }
  
  public void setPageNr(int pageNr) {
    pageNrPhone = pageNr;
    pageNrScreen = pageNr;
  }
  
  public int getMaxPages () {
      return maxPages;
  }
}

